"""
推荐反馈模型
"""
from datetime import datetime
from app.extensions import db


class RecommendationFeedback(db.Model):
    """推荐反馈表"""
    __tablename__ = 'recommendation_feedback'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True)
    course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False, index=True)
    recommendation_id = db.Column(db.String(50))
    feedback_type = db.Column(db.Enum('like', 'dislike', 'enrolled', 'ignored'), nullable=False)
    rating = db.Column(db.Integer)
    comment = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True)
    
    # 关系
    user = db.relationship('User', backref='recommendation_feedbacks')
    course = db.relationship('Course', backref='recommendation_feedbacks')
    
    __table_args__ = (
        db.Index('idx_user_course_feedback', 'user_id', 'course_id'),
        db.Index('idx_created_at_desc', 'created_at', postgresql_using='btree'),
        db.CheckConstraint('rating >= 1 AND rating <= 5', name='chk_rating_range'),
    )
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'course_id': self.course_id,
            'recommendation_id': self.recommendation_id,
            'feedback_type': self.feedback_type,
            'rating': self.rating,
            'comment': self.comment,
            'created_at': self.created_at.isoformat() if self.created_at else None,
        }










